import 'package:amap_flutter_location/amap_flutter_location.dart';
import 'package:amap_flutter_location/amap_location_option.dart';
import 'package:base_flutter/common/global.dart';
import 'package:base_flutter/models/index.dart';
import 'package:base_flutter/states/base_change_notifier.dart';
import 'package:flutter/material.dart';
import 'package:geolocator/geolocator.dart';
import 'package:provider/provider.dart';

import 'home.dart';


class Index extends StatefulWidget {
  const Index({Key? key}) : super(key: key);

  @override
  State<StatefulWidget> createState() {
    // TODO: implement createState
    return _IndexState();
  }
}

class _IndexState extends State<Index> with SingleTickerProviderStateMixin {
  late PageController _tabController;

  late List<Widget> screens;
  final GeolocatorPlatform _geolocatorPlatform = GeolocatorPlatform.instance;
  AMapFlutterLocation? _location;

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    /// 1. 位置信息

    /// 2. 隐私协议
    debugInfo('_IndexState initState');

    _location = AMapFlutterLocation();

    if (!Global.isOther) {
      _location?.setLocationOption(AMapLocationOption(distanceFilter: 20, onceLocation: true, desiredAccuracy: DesiredAccuracy.NearestTenMeters));
      debugInfo('开始定位');
      _location?.onLocationChanged().listen((event) {
        debugInfo('定位成功');
        debugInfo(event);
        Provider.of<UserLocation>(context, listen: false).location = User_location.fromJson(event);

      });
      _location?.startLocation();
    }else {
      _requesLocation();
    }

  }

  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    debugInfo('_IndexState');

    return const Material(child: Home(),);

  }

  Future<bool> _handlePermission() async {
    bool serviceEnabled;
    LocationPermission permission;

    // Test if location services are enabled.
    serviceEnabled = await GeolocatorPlatform.instance.isLocationServiceEnabled();
    if (!serviceEnabled) {


      return false;
    }

    permission = await GeolocatorPlatform.instance.checkPermission();
    if (permission == LocationPermission.denied) {
      permission = await GeolocatorPlatform.instance.requestPermission();
      if (permission == LocationPermission.denied) {
        // Permissions are denied, next time you could try
        // requesting permissions again (this is also where
        // Android's shouldShowRequestPermissionRationale
        // returned true. According to Android guidelines
        // your App should show an explanatory UI now.

        return false;
      }
    }

    if (permission == LocationPermission.deniedForever) {
      // Permissions are denied forever, handle appropriately.


      return false;
    }

    // When we reach here, permissions are granted and we can
    // continue accessing the position of the device.

    return true;
  }

  void _requesLocation() async{
    debugInfo('position:_requesLocation');

    final hasPermission = await _handlePermission();

    if (!hasPermission) {
      debugInfo('position:没有权限');
      return;
    }
    debugInfo('position:有权限');
    try {
      Position position = await _geolocatorPlatform.getCurrentPosition(locationSettings: LocationSettings());
      debugInfo('position:$position');
    }catch (e) {
      debugInfo('positionE:$e');
    }

  }
  @override
  void dispose() {
    // TODO: implement dispose
    super.dispose();

    _location?.stopLocation();
    _location?.destroy();
    _tabController.dispose();
  }
}
